package cn.edu.swu.mysqlite

import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.ContentValues.TAG
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class Todo: AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_todo)

//        Log.d(TAG,"onActivityCreated");
        initDatabase()
    }

    private fun initDatabase() {
        val dbHelper = MyDatabaseHelperTodo(this, "TodoList.db", 2)
        val createTodo: Button = findViewById(R.id.createTodo)
        createTodo.setOnClickListener {
            dbHelper.writableDatabase
            Log.d("todo", "dbcreate")
        }
        addData(dbHelper)
        showData(dbHelper)
        deleteData(dbHelper)
        updateData(dbHelper)
    }

    private fun updateData(dbHelper: MyDatabaseHelperTodo) {
        val updateData: Button = findViewById(R.id.updateDataBtn)
        val inputName: EditText = findViewById(R.id.updateNameEdit)
        val inputRecord: EditText = findViewById(R.id.updateRecordEdit)
        updateData.setOnClickListener {
            val db = dbHelper.writableDatabase
            val values = ContentValues()
            values.put("record", inputRecord.text.toString())
            db.update("Todo", values, "name = ?", arrayOf(inputName.text.toString()))
//            values.put("record", "hello world")
//            db.update("Todo", values, "name = ?", arrayOf("lxy"))
        }
    }

    private fun deleteData(dbHelper: MyDatabaseHelperTodo) {
        val deleteData: Button = findViewById(R.id.deleteInfoBtn)
        val deleteNameEdit: EditText = findViewById(R.id.deleteNameEdit)
        deleteData.setOnClickListener {
            val db = dbHelper.writableDatabase
//            db.delete("Todo", "name = ?", arrayOf(deleteNameEdit.text.toString()))
            db.delete("Todo", "name = ?", arrayOf("lxy"))
        }
    }

    @SuppressLint("Range")
    private fun showData(dbHelper: MyDatabaseHelperTodo) {
        val queryData: View = findViewById(R.id.showDataBtn)
        val show: TextView = findViewById(R.id.showBox)
        var showContent: String = "name todo\n"
        queryData.setOnClickListener {
            val db = dbHelper.writableDatabase
            // 查询Book表中所有的数据
            val cursor = db.query("Todo", null, null, null, null, null, null)
            if (cursor.moveToFirst()) {
                do {
                    // 遍历Cursor对象，取出数据并打印
                    val name = cursor.getString(cursor.getColumnIndex("name"))
                    val record = cursor.getString(cursor.getColumnIndex("record"))
                    showContent += name
                    showContent += ' '
                    showContent += record
                    showContent += '\n'
                    Log.d("TodoActivity", "name is $name")
                    Log.d("TodoActivity", "record is $record")
                } while (cursor.moveToNext())
            }
            show.text = showContent
            cursor.close()
        }
    }

    private fun addData(dbHelper: MyDatabaseHelperTodo) {
        val addData: Button = findViewById(R.id.addDataBtn)
        val inputName: EditText = findViewById(R.id.addNameEdit)
        val inputTodo: EditText = findViewById(R.id.addTodoEdit)
        addData.setOnClickListener {
            val db = dbHelper.writableDatabase
            val values1 = ContentValues().apply {
                // 开始组装第一条数据

                put("record", inputTodo.text.toString())
                put("name", inputName.text.toString())
//                put("name", "lxy")
//                put("record", "todo")

            }
            db.insert("Todo", null, values1) // 插入数据
        }
    }
}